//
//  DBBRegistVC.m
//  夺宝吧
//
//  Created by xiaocai on 15/6/19.
//  Copyright (c) 2015年 FangYuan. All rights reserved.
//

#import "DBBRegistVC.h"
#import "YanZhengMaVC.h"
#import "DBBLoginVC.h"
#import "WYPublicWeb.h"
#import "RegistQuestionVC.h"
//注册检验接口
#define CHECK_URL @"/api/ApiUser/registerCheck"
//协议
#define ATCL_URL @"api/ApiMisc/article?id="
@interface DBBRegistVC ()<UITextFieldDelegate>
{
    //背景view
    __weak IBOutlet UIView *bgView;
    //账号
    __weak IBOutlet UITextField *countLab;
    //密码
    __weak IBOutlet UITextField *pwdLab;
    //密码
    __weak IBOutlet UITextField *ensurePwdLab;
    //下一步按钮
    __weak IBOutlet UIButton *nextBtn;
    //勾选按钮
    __weak IBOutlet UIButton *selectBtn;
    
}
@end

@implementation DBBRegistVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self reloadSubViews];
}
//界面处理
- (void)reloadSubViews
{
    nextBtn.layer.masksToBounds = YES;
    nextBtn.layer.cornerRadius = 2;
    pwdLab.delegate = self;
    ensurePwdLab.delegate = self;
    pwdLab.secureTextEntry = YES;
    ensurePwdLab.secureTextEntry = YES;
    [selectBtn setImage:[UIImage imageNamed:@"icon_checkbox_press"] forState:UIControlStateSelected];
    selectBtn.selected = YES;
    //屏蔽键盘事件
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeKeyBoard)];
    [self.view addGestureRecognizer:tap];
}
- (void)awakeFromNib
{
    self.view.frame = CGRectMake(0,0,WYMWITH, WYMHEIGHT);
}
//关闭键盘
- (void)closeKeyBoard
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

//返回按钮的监听事件
- (IBAction)backBtnOnClick:(id)sender {
//    if (self.father == nil) {
//        [WYPublic logOrRegistSuccess];
//    }
//    [self dismissViewControllerAnimated:YES completion:nil];
    
    [self.navigationController popViewControllerAnimated:YES];
}
//勾选按钮的监听事件
- (IBAction)selectBtnOnClick:(id)sender {
    selectBtn.selected = !selectBtn.selected;
}

//下一步按钮的监听事件
- (IBAction)nextBtnOnCLick:(id)sender {
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    if(countLab.text.length == 0)
    {
        [WYPublic showMessageWithTitle:nil Msg:@"请输入手机号！"];
        return;
    }
    if(![WYPublic isPhoneNum:countLab.text]){
        return;
    }
    if(pwdLab.text.length == 0)
    {
        [WYPublic showMessageWithTitle:nil Msg:@"请输入注册密码！"];
        return;
    }
    if(ensurePwdLab.text.length == 0)
    {
        [WYPublic showMessageWithTitle:nil Msg:@"请输入确认密码！"];
        return;
    }
    if (pwdLab.text.length < 6) {
        [WYPublic showMessageWithTitle:nil Msg:@"密码太短！"];
        return;
    }else if (pwdLab.text.length > 20) {
        [WYPublic showMessageWithTitle:nil Msg:@"密码太长！"];
        return;
    }
    if(![ensurePwdLab.text isEqualToString:pwdLab.text])
    {
        [WYPublic showMessageWithTitle:nil Msg:@"两次输入密码不一致！"];
        return;
    }
    if(selectBtn.selected == NO)
    {
        [WYPublic showMessageWithTitle:nil Msg:@"请阅读并同意《夺宝吧协议》！"];
        return;
    }
    
    //检验用户注册信息
    [self checkUserInfo];
}
//已有账号按钮
- (IBAction)areadyBtnOnClick:(id)sender {
//    DBBLoginVC *log = [[DBBLoginVC alloc]init];
//    WINDOW.rootViewController = log;
    
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//检验用户注册信息
- (void)checkUserInfo
{
    [WYPublic addZheZhaoToWindowWithTitle:nil];
    NSDictionary *params = @{@"account":countLab.text,@"password":pwdLab.text,@"confirm_password":ensurePwdLab.text,@"auto_send_verify":@"0"};
    [WYRequest startPostForDicWithUrlStR:[NSString stringWithFormat:@"%@%@",DBB_IP,CHECK_URL] Parameters:params successBlock:^(NSDictionary *dic) {
        NSNumber *ret = dic[@"ret"];
        if (ret.intValue == 0) {
            //跳往验证码界面
            YanZhengMaVC *yanzhengmaVC = [[YanZhengMaVC alloc]init];
            yanzhengmaVC.phone = countLab.text;
            yanzhengmaVC.pwd = pwdLab.text;
            yanzhengmaVC.ensure_pwd = ensurePwdLab.text;
//            [self presentViewController:yanzhengmaVC animated:YES completion:nil];
            [self.navigationController pushViewController:yanzhengmaVC animated:YES];
        }else{
            [WYPublic showMessageWithTitle:nil Msg:dic[@"msg"]];
        }
        [WYPublic removeZheZhaoToWindow];
    } failedBlock:^{
        [WYPublic removeZheZhaoToWindow];
    } needLog:NO];
   
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark -- 输入框协议函数
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.20 animations:^{
        bgView.frame = CGRectMake(bgView.frame.origin.x, -100, bgView.frame.size.width, bgView.frame.size.height);
    }];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.20 animations:^{
        bgView.frame = CGRectMake(bgView.frame.origin.x, 0, bgView.frame.size.width, bgView.frame.size.height);
    }];
}
//服务协议
- (IBAction)xieyi:(id)sender {
    WYPublicWeb *web = [[WYPublicWeb alloc]init];
    web.mtitle = @"服务协议";
    NSString *newIp = [DBB_IP stringByReplacingOccurrencesOfString:@"?/" withString:@""];
    web.urlStr = [NSString stringWithFormat:@"%@%@%@",newIp,ATCL_URL,@"dbba_service"];
    web.isDIsmissFLag = YES;
    [self presentViewController:web animated:YES completion:nil];
}
//注册问题
- (IBAction)zcwt:(id)sender {
    RegistQuestionVC *registerVC = [[RegistQuestionVC alloc]init];
    [self presentViewController:registerVC animated:YES completion:nil];
}


@end
